package br.com.rpinfo.helpdesk;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author camilo
 */
public class ClienteDaoHibernate implements ClienteDao {

  private Session session;
  private Transaction transacao;

  public void setSession(Session session) {
    this.session = session;
  }

  @Override
  public void salvar(Cliente cliente) {
    this.session.saveOrUpdate(cliente);
  }

  @Override
  public void excluir(Cliente cliente) {
    this.session.delete(cliente);
  }

  @Override
  public Cliente carregar(int id) {
    return (Cliente) this.session.get(Cliente.class, id);
  }

  @Override
  public List<Cliente> buscarPorNome(String nome) {
    Criteria criteria = this.session.createCriteria(Cliente.class);
    criteria.add(Restrictions.ilike(Cliente.ATRIBBUTE_NOME, nome));
    return (List<Cliente>) criteria.list();
  }

  @Override
  public List<Cliente> listar() {
    Criteria criteria = this.session.createCriteria(Cliente.class);
    criteria.addOrder(Order.asc(NAME_COLUMN_ID));
    return criteria.list();
  }
}
